<?php
/*date_default_timezone_set(  'Asia/Shanghai'  );
$targetFolder = '/upload/';
if (!empty($_FILES)) {
    $file_name = iconv("UTF-8","gb2312", $_FILES['file']['name']); //文件名称
    $filenames= explode(".",$file_name);
    $tempFile = $_FILES['file']['tmp_name'];
    $rand = rand(1000, 9999);
    $targetPath = ltrim($targetFolder,'/'); //图片存放目录
    $targetFile = rtrim($targetPath,'/') . '/' .time().$rand.".".$filenames[count($filenames)-1]; //图片完整路徑

    // Validate the file type
    $fileTypes = array('jpg', 'jpeg', 'png'); // File extensions
    $fileParts = pathinfo($_FILES['file']['name']);

    if (in_array($fileParts['extension'],$fileTypes)) {
        move_uploaded_file($tempFile,iconv("UTF-8","gb2312", $targetFile));
        exit(json_encode(array("url"=>$targetFile,'name'=>$file_name)));
    } else {
        echo 'Invalid file type.';
    }
}*/

if($_FILES['file']['error'] == 0) {
    $name = iconv("UTF-8", "gbk", $_FILES['file']['name']);
    $ext = strtolower(substr($name, strrpos($name, '.') + 1));
    $tmp = $_FILES['file']['tmp_name'];
    $ext_check = array('jpg', 'png', 'jpeg');
    $size = ceil($_FILES['file']['size'] / 1024);
    //设置上传路径，这里默认是uploads文件夹

    $chider= date('Ymd',time());
    if(!file_exists($chider))//文件夹不存在，先生成文件夹
    {
        mkdir($chider);
    }
    //文件名称
    $file_name = uniqid().'.'.$ext;
    if (!in_array($ext, $ext_check)) {
        exit(json_encode(array('code' => '01', 'msg' =>$ext)));
    }else{
        //限制单张图片不能超过8M
        if($size>8192) {
            exit(json_encode(array('code' => '02', 'msg' => '文件超出限制8M')));
        } else {
            move_uploaded_file($tmp,iconv("UTF-8","gb2312",  $chider."/".$file_name));
            exit(json_encode(array('code' => '00', 'msg' => $chider."/".$file_name)));
        }
    }
}else{
    exit(json_encode(array('code' => '99', 'msg' => '上传失败，Error99！')));
}
